package com.jiejie.controller;


import com.jiejie.commons.ServerResponse;
import com.jiejie.entity.*;
import com.jiejie.service.*;
import com.jiejie.util.TokenUtil;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 辰广
 * @since 2022-10-05
 */
@RestController
public class MovieController {

    @Resource
    private IMovieService movieService;
    @Resource
    private ICommentsService commentsService;
    @Resource
    private IMovieAndCastService movieAndCastService;
    @Resource
    private ICouponService couponService;
    @GetMapping("MovieDetail")
    public ModelAndView getMovieDetail(@RequestParam("movieId")Integer movieId){
       /* System.out.println("movieId:"+movieId+",token:"+token);
        LoginCustomer loginCustomer =null;
        if (token!=null) {
            //1.登陆用户数据
            loginCustomer = TokenUtil.parseToken(token);
            Integer customerId = loginCustomer.getCustomerId();
            String customerName = loginCustomer.getCustomerName();

        }*/
        Movie movie=movieService.getMovieDetailById(movieId);
        //System.out.println("1111111111");
        Map<Comments, Customer> commentsAndCustomerList=commentsService.getCommentsByMovieId(movieId);
        //System.out.println("2222222222222");
        float allCommentsScore=commentsService.getAllCommentsScore(movieId);
        String[] strType=movie.getMovieType().split(" ");
        List<String> listType = new ArrayList<>(Arrays.asList(strType));
        //System.out.println(listType);
        //System.out.println("33333333");
        Map<MovieAndCast,Cast> movieAndCastYYCastMap= movieAndCastService.getCastYYByMovidId(movieId);
        Map<MovieAndCast,Cast> movieAndCastDYCastMap= movieAndCastService.getCastDYByMovidId(movieId);
        ModelAndView mav=new ModelAndView();
        //用户数据
        //mav.addObject("loginCustomer",loginCustomer);
        //电影信息
        mav.addObject("movie",movie);
        //电影类型
        mav.addObject("listType",listType);
        //评价表
        mav.addObject("commentsAndCustomerList",commentsAndCustomerList);
        //总评分
        mav.addObject("allCommentsScore",allCommentsScore);
        //演员表
        mav.addObject("movieAndCastYYCastMap",movieAndCastYYCastMap);
        //导演
        mav.addObject("movieAndCastDYCastMap",movieAndCastDYCastMap);
        //收藏

        mav.setViewName("pages/XQing");
        return mav;
    }

    @GetMapping("XQing")
    public ServerResponse loadIndexData(HttpServletRequest request){
        String token=request.getHeader("token");
        LoginCustomer loginCustomer =null;
        if (!token.equals("null")) {
            //1.登陆用户数据6
            loginCustomer = TokenUtil.parseToken(token);
            Integer customerId = loginCustomer.getCustomerId();
            String customerName = loginCustomer.getCustomerName();
        }
        return ServerResponse.success("ok",loginCustomer);
    }

    @GetMapping("films")
    public ServerResponse getFilms(String movieStatus,String movieType,String movieArea,String movieOpenhours,Integer page){
        System.out.println("terms"+movieStatus);
        ServerResponse response = movieService.getMovieFilms(Integer.parseInt(movieStatus),movieType,movieArea,movieOpenhours,page);
        System.out.println("查询到的数据:"+response.getData());
        return response;
    }

    @GetMapping("msg")
    public ModelAndView getOrderAndMovie(@RequestParam("customerId") Integer customerId) {
        System.out.println("获得用户id:" + customerId);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("OrderAndMovie", movieService.getOrderMovie(customerId));
        modelAndView.addObject("message", "消息通知");
        modelAndView.setViewName("pages/profile_msg");
        System.out.println("dsfd:" + movieService.getOrderMovie(customerId));
        return modelAndView;
    }

    @GetMapping("confirmCoupon")
    public Map<String,Object> getConfirmCoupon(HttpServletRequest request){
        String token=request.getHeader("token");
        System.out.println("token:"+token);
        LoginCustomer loginCustomer =null;
        if (!token.equals("null")) {
            //1.登陆用户数据
            loginCustomer = TokenUtil.parseToken(token);
            Integer customerId = loginCustomer.getCustomerId();
            String customerName = loginCustomer.getCustomerName();
        }
        System.out.println("========================"+loginCustomer.getCustomerId());
        return couponService.getConfirm(loginCustomer.getCustomerId());
    }

}
